package com.platform.modules.order;

import com.platform.common.utils.Constant;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.entity.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

public class OrderUtil {

    /**
     * @param params  下单参数
     * @param order   订单
     * @param entity 商品
     * @return
     */
    public static List<MallOrderGoodsEntity> goodsToOrderGoodsList(ParamsEntity params, MallOrderEntity order, MallGoodsEntity entity) {
        List<MallOrderGoodsEntity> orderGoodsData = new ArrayList<>();
            MallOrderGoodsEntity orderGoodsVo = new MallOrderGoodsEntity();
            orderGoodsVo.setOrderId(order.getId());
            orderGoodsVo.setGoodsId(entity.getId());
            orderGoodsVo.setGoodsSn(entity.getGoodsSn());
            orderGoodsVo.setSkuId(params.getSkuId());
            orderGoodsVo.setGoodsName(entity.getName());
            orderGoodsVo.setListPicUrl(entity.getListPicUrl());
            orderGoodsVo.setMarketPrice(entity.getMarketPrice());
            orderGoodsVo.setRetailPrice(entity.getRetailPrice());
            orderGoodsVo.setNumber(params.getNumber());
            // 设置微信优惠标识
            orderGoodsVo.setCouponTag(entity.getGoodsTag());
            orderGoodsData.add(orderGoodsVo);
        return orderGoodsData;
    }

    /**
     * @param params  下单参数
     * @param order   订单
     * @param entitys 商品数组
     * @return
     */
    public static List<MallOrderGoodsEntity> goodsToOrderGoodsList(ParamsEntity params, MallOrderEntity order, List<MallGoodsEntity> entitys) {
        List<MallOrderGoodsEntity> orderGoodsData = new ArrayList<>();
        entitys.stream().forEach(x -> {
            MallOrderGoodsEntity orderGoodsVo = new MallOrderGoodsEntity();
            orderGoodsVo.setOrderId(order.getId());
            orderGoodsVo.setGoodsId(x.getId());
            orderGoodsVo.setGoodsSn(x.getGoodsSn());
            orderGoodsVo.setSkuId(params.getSkuId());
            orderGoodsVo.setGoodsName(x.getName());
            orderGoodsVo.setListPicUrl(x.getListPicUrl());
            orderGoodsVo.setMarketPrice(x.getMarketPrice());
            orderGoodsVo.setRetailPrice(x.getRetailPrice());
            orderGoodsVo.setNumber(params.getNumber());
            // 设置微信优惠标识
            orderGoodsVo.setCouponTag(x.getGoodsTag());
            orderGoodsData.add(orderGoodsVo);
        });
        return orderGoodsData;
    }

    /**
     * 商品对象 类 转购物车对象
     *
     * @param x
     * @param number
     * @param skuId
     * @return
     */
    public static MallCartEntity goodsToCartEntity(MallGoodsEntity x, Integer number, String skuId) {
        MallCartEntity temp = new MallCartEntity();
        temp.setRetailPrice(x.getRetailPrice());
        temp.setGoodsName(x.getName());
        temp.setNumber(number);
        temp.setGoodsId(x.getId());
        temp.setListPicUrl(x.getListPicUrl());
        temp.setSkuId(skuId);
        temp.setGoodsSn(x.getGoodsSn());
        temp.setGoodsEntity(x);
        return temp;
    }

    public static MallOrderEntity getOrder(MallGoodsEntity entity, ParamsEntity params, MallUserEntity loginUser) {
        String orderSn = StringUtils.generateOrderNumber();
        BigDecimal goodsTotalPrice = Optional.ofNullable(entity)
                .map(x->x.getRetailPrice().multiply(BigDecimal.valueOf(params.getNumber())))
                .orElse(BigDecimal.ZERO);
        Date date = new Date();
        MallOrderEntity orderInfo = new MallOrderEntity();
        // 设置默认的手机号
        orderInfo.setMobile(loginUser.getMobile());
        orderInfo.setActivityId(params.getActivityId());
        orderInfo.setId(orderSn);
        orderInfo.setOrderSn(orderSn);
        orderInfo.setUserId(loginUser.getId());
        orderInfo.setCouponPrice(BigDecimal.ZERO);
//        orderInfo.setActualPrice(goods.getRetailPrice());
        orderInfo.setShopsId(params.getShopsId());
        orderInfo.setFromType(params.getFromType());
        //留言
        orderInfo.setPostscript(params.getPostscript());
        orderInfo.setAddTime(date);
        orderInfo.setExpireTime(DateUtils.addDateMinutes(date, 30));
        orderInfo.setOrderPrice(goodsTotalPrice);
        orderInfo.setActualPrice(goodsTotalPrice);
        // 待付款
        orderInfo.setOrderStatus(Constant.OrderStatus.DFK.getValue());
        orderInfo.setShippingStatus(Constant.ShippingStatus.WFH.getValue());
        orderInfo.setPayStatus(Constant.PayStatus.WFK.getValue());
        orderInfo.setShippingFee(BigDecimal.ZERO);
        orderInfo.setIntegralMoney(BigDecimal.ZERO);
        orderInfo.setActivityType(entity == null ? null : entity.getGoodsTag());

        return orderInfo;
    }

}
